package com.example.producer.listener;

import java.util.concurrent.TimeUnit;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DaemonThreadDemo {

    public static void main(String[] args) {
        log.info("当前线程：{}", Thread.currentThread().getName());
        //创建一个用户线程，一直运行
        Thread thread = new Thread(() -> {
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                    log.info("当前线程：【{}】,正在运行", Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    log.error("当前线程：{}，休眠异常：{}", Thread.currentThread().getName(), e);
                }
            }
        }, "子线程");

        //启动线程
        thread.start();

        //主线程休眠2s
        try {
            TimeUnit.SECONDS.sleep(2);
            log.info("主线程：{},休眠2s", Thread.currentThread().getName());
        } catch (InterruptedException e) {
            log.error("主线程:{}休眠异常：{}", Thread.currentThread().getName(), e);
        }

        //主线程结束，打印信息
        log.info("主线程：{},结束运行", Thread.currentThread().getName());
    }

}
